import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    Button,
    Image,
    TouchableOpacity,
    ScrollView,
    AsyncStorage
} from 'react-native';

import keyData from '../../res/data/keys';
import langsData from '../../res/data/langs';

export var FLAG_LANGUAGE = {flag_language:"flag_language_language",flag_key:"flag_language_key"}
export default class LaunageDao {
   constructor(flag){
       this.flag = flag;
   }
   fetchData(){
       return new Promise((resolve, reject)=>{
           AsyncStorage.getItem(this.flag,(error, result)=>{
               if(error) {
                   reject(error)
               } else {
                   if (result) {
                       try {
                           resolve(JSON.parse(result));
                       } catch (e){
                           reject(e)
                       }
                   } else {
                       var data = this.flag === FLAG_LANGUAGE.flag_language ? langsData :keyData;
                       this.save(data);
                       resolve(data);
                   }
               }
           })
       });
   }
   save(data){
       AsyncStorage.setItem(this.flag, JSON.stringify(data),(error)=>{

       })
   }
}